<?php

/**
 * 这个是按经纪人切换显示内容
 */
global $_GPC, $_W;
$uniacid = $this->_uniacid;
$bid = $_GPC['bid'];
$keyword = $_GPC['keyword'];
$openid = $_W['openid'];
$uid = $_W['member']['uid'];
if (empty($uid)) {
    $fans = mc_fansinfo($openid, 0, $uniacid);
    $uid = $fans['uid'];
}

if ($_GPC['enews'] == 'changeBroker') {
    $data = pdo_fetch("select * from " . tablename("kbwdl_broker") .
            " where uniacid='$uniacid' and openid='$openid'");
    if (empty($data)) {
        $save['openid'] = $openid;
        $save['uniacid'] = $uniacid;
        $save['ecuid'] = $uid;
        $save['groupid'] = intval($_GPC['utype']);
        pdo_insert("kbwdl_broker", $save);
    } else {

        if ($_GPC['utype'] == 0) {
            $update['groupid'] = 0;
        } else {
            $update['groupid'] = 0;
            $update['buygroup_log_id'] = 1;
            $update['ischecked'] = 0;
        }
        pdo_update('kbwdl_broker', $update, array('openid' => $openid, 'uniacid' => $uniacid));
    }

    $this->result(0, "经纪人资料", $data);
}
/* 修过备案进度 */
if ($_GPC['enews'] == 'modify') {
    $id = intval($_GPC['actid']);
    $step = intval($_GPC['step']);
    $update = array(
        'last_time' => TIMESTAMP,
        'hits' => $step
    );
    pdo_update('kbwdl_favorite', $update, array(  'id' => $id, 'uniacid' => $uniacid));
    $info = pdo_fetch("select openid,title,smalltext from ".  tablename("kbwdl_favorite") . " where id='$id' and uniacid='$uniacid'");
    /* 添加到操作日志 */
    $step_name = $this->_forms['charges_step'][$step];
    $save = array(
        'actname' => 'chargestep',
        'ecuid' => $uid,
        'addtime' => date('Y-m-d H:i:s', TIMESTAMP),
        'infoid' => $id,
        'note' => $step_name,
        'uniacid' => $uniacid,
        'acttype' => 31,
        'num'=> $step, /*当前的进度*/
        'mark' => '{"operation":"项目助理","openid":"'.$openid.'"}'
    );
    pdo_insert('kbwdl_actlog', $save);
    //$data = array('keyword1'=>array('value'=> '推荐提醒 '.$step_name),'keyword2'=>array('value'=>$info['smalltext'].",请及时查看。 "));
   // include  IA_ROOT.  "/addons/kbwdl_shequzufang/vender/mnshelper.php";
   // $helper = new mnshelper();
    //$formIds = $helper->getFormId($info['openid']);
    $formIds = 'baobei_status';
    $data = array('keyword1'=>array('value'=>'报备流程进度处理'),
          'keyword2'=>array('value'=>$info['title']/*.'|'.$savedata['smalltext']*/),
          'keyword3'=>array('value'=> explode("|",$info['smalltext'] )),
          'keyword4'=>array('value'=> $step_name)
      );
      
    if(!empty($formIds)){
        $ret =  $this->_send_template_notice($info['openid'], 'kbwdl_shequzufang/pages/index/user', $formIds, $data);
    }

    $this->result(0, "修改成功", $update);
}
/* 查询 列表信息 */
if ($_GPC['enews'] == 'details') {
    $id = intval($_GPC['actid']);
    $ret = pdo_fetch('select * from ' . tablename('kbwdl_favorite') . ' where id=:id  and uniacid=:uniacid', array(  ':id' => $id, ':uniacid' => $uniacid));
    $ret['truename'] = '--';
    $ret['mobile'] =  $ret['hide_mobile'] =  '--';
    $sec = $this->_forms['charges_step'];
    if ($ret['hits']) {
        $ret['step'] = $sec[$ret['hits']];
    } else {
        $ret['step'] = '推荐中';
    }
    if (!empty($ret['jsondata'])) {
        $jsd = json_decode($ret['jsondata'], true);
        $ret['truename'] = $jsd['truename'];
        $ret['mobile'] = $jsd['mobile'];
        if($jsd['mobile']){
                   $ret['hide_mobile'] = preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2',$jsd['mobile']);
               }
        $ret['jsondata'] = '';
    }
    $data['info'] = $ret;
    $data['logs'] = pdo_fetchall("select id,addtime, note,mark from " . tablename('kbwdl_actlog') . " where infoid='$id' and actname='chargestep' order by id desc");
    /*获取经纪人*/ 
    if($ret['openid']){
        $data['broker'] = pdo_fetch("select nickname,mobile,vtags,company from ".tablename("kbwdl_broker")." where openid='".$ret['openid']."'");
    }

    $this->result(0, "详情", $data);
}
/**
 * 获取项目经理和助理的预约信息
 */
if($_GPC['enews'] == 'myfeed'){
       $limit = 50;
    if (isset($_GPC['limit']) && !empty($_GPC['limit'])) {
        $limit = intval($_GPC['limit']);
    }
    
   
    $broker = pdo_fetch("select id,openid,nickname,groupid,cgid from ".tablename("kbwdl_broker")." where openid='$openid'");
    $bid = $broker['id'];
    $sql_add = " uniacid='$uniacid'   and acttype='feed' and ftype=8 and hits=0 and bid='$bid' ";
     /*查询 助理和经理的情况*/
    if($broker['cgid']==1){
        //项目助理       
        $sql_add = " uniacid='$uniacid' and acttype='feed' and ftype=8 and hits=0  and  houseid in (select newshouse_id from ".tablename('kbwdl_house_info')." where cgid1='$bid') ";
    }
     if($broker['cgid']==2){        //项目经理
       
        $sql_add = " uniacid='$uniacid' and acttype='feed' and ftype=8 and hits=0  and  houseid in (select newshouse_id from ".tablename('kbwdl_house_info')." where cgid2='$bid') ";
    }
 
    $keyword = trim($_GPC['keyword']);
    $queradd = "";
    if($keyword){
        $queradd = " and  smalltext like '%$keyword%' ";
    }
      
    $allsql = "select *  from " . tablename("kbwdl_favorite") . "   where $sql_add  $queradd order by  addtime desc limit  50 ";
    $ret = pdo_fetchall( $allsql );
    $data = [];
    foreach($ret as $key => $val){
        $val['url'] =   '/kbwdl_shequzufang/pages/newshop/index?id='.$val['houseid']; 
        $val['addtime'] = date('Y-m-d H:i:s', $val['addtime']);
        $val['typename'] = '新房';
        $data[] = $val;
    }
    $sql = "select * from ". tablename("kbwdl_favorite") . "   where acttype='feed' and ftype=3 and hits=0 and  uniacid='$uniacid'  and bid='$bid'  $queradd  order by  addtime desc limit  50 ";
    $ret = pdo_fetchall($sql);
     foreach($ret as $key => $val){
        $val['url'] =  '/kbwdl_shequzufang/pages/house/index?id='.$val['houseid'];    
        $val['addtime'] = date('Y-m-d H:i:s', $val['addtime']);
        $val['typename'] = '二手房';
        $data[] = $val;
    }   
   
    $this->result(0, 'success', $data);    
}
/*申请佣金提现*/
if($_GPC['enews'] == 'agentMoney'){
    $broker = pdo_fetch("select id from ".tablename("kbwdl_broker")." where openid='$openid' and uniacid='$uniacid'");
    if(empty($broker)){
        $this->result(1, '申请失败', array()); 
    }
    /*查询申请记录*/
 
    $has = pdo_fetchcolumn("select id from ".tablename("kbwdl_actlog")." where mark='$openid' and num=1 and uniacid='$uniacid'");
    if($has){
        $this->result(1, '已经有申请待处理中！', array()); 
    }
    $save = array(
        'actname'=> 'agentMoney',
        'addtime' => date('Y-m-d H:i:s', TIMESTAMP),
        'uniacid'=> $uniacid,
        'acttype'=> 51,
        'money'=> $_GPC['money'],
        'isadd'=>0,
        'note'=> '申请佣金提现',
        'num' => 1,/*1 为待处理*/
        'mark'=> $openid,
        'infoid'=>0
    );
    pdo_insert('kbwdl_actlog', $save);
    $this->result(0, 'success', $save);    
}
?>
